package orfeu.engine;

import AI.Hell;
import AI.Hero;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;


public class SpriteGameActivity extends Activity {
	// private OpenGL ES View member
	private GLView view;
	public static Context context;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        view = new GLView(getApplicationContext(), this);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        //Set the screen to remain
        view.setKeepScreenOn(true);
        
        Constants.screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        Constants.screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        if(getWindowManager().getDefaultDisplay().getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)//Cazul in care suntem pe tableta
        	view.landscape = true;
        else view.landscape = false;
       
        //Constante pentur OpenGL
        if(view.landscape){//Daca suntem pe cazul tableta, merg astea:
        	Constants.limitRight = 700.0f*((350.0f/700.0f))*Constants.screenWidth/1280.0f;
        	Constants.limitLeft = -(700.0f*((350.0f/700.0f))*Constants.screenWidth/1280.0f);
        	Constants.limitUp = 200.0f;
        	Constants.limitDown = -200.0f;
        }
        else{ //daca suntem pe cazul unui telefon
        	Constants.limitRight = 700.0f*((550.0f/700.0f))*Constants.screenWidth/1280.0f;
        	Constants.limitLeft = -(700.0f*((550.0f/700.0f))*Constants.screenWidth/1280.0f);
        	Constants.limitUp = 200.0f;
        	Constants.limitDown = -200.0f;
        	}
        	
        
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(view);
		
    }
    
    @Override
    public void onStop()
    {
    	//resetam tot
    	Hero.reset();
    	GLView.reset();
    	((Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE)).cancel();
    	Hell.myaudioclip.stop();
    	
    	//Activam highscores
    	if(GLView.canEnterHigh){
    		Intent myIntent = new Intent(SpriteGameActivity.this,AddHighscore.class);
    		startActivity(myIntent);
    	}
    	System.runFinalizersOnExit(true);
    	
    	super.onStop();   	
    }
    
    @Override
    public void onDestroy()
    {
    	Hero.reset();
    	GLView.reset();
    	((Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE)).cancel();
    	Hell.myaudioclip.stop();
    	//Activam Highscores
    	if(GLView.canEnterHigh){
    		Intent myIntent = new Intent(SpriteGameActivity.this,AddHighscore.class);
    		startActivity(myIntent);
    	}
    	super.onDestroy();
    }
    
    public void onRestart()
    {
    	super.onRestart();
    	Hero.reset();
    	GLView.reset();
    	((Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE)).cancel();
    	Hell.myaudioclip.stop();
    }
}